package zhongbo.java.air.monitor.Service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.monitor.Service.IFlightActivityDailyService;
import zhongbo.java.air.monitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.monitor.model.FlightActivityDaily;

import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl extends ServiceImpl<FlightActivityDailyMapper, FlightActivityDaily> implements IFlightActivityDailyService {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
        }
        // todo 根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回
        LambdaQueryWrapper<FlightActivityDaily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FlightActivityDaily::getDate, date);
        return flightActivityDailyMapper.selectList(queryWrapper);
    }

    @Override
    public PageVo<FlightActivityDaily> pageQuery(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        Page<FlightActivityDaily> flightActivityDailyPage = new Page<>(page,pageSize);
        if (oderByField == null || oderByField.isEmpty()){
            queryWrapper.orderByAsc("updated_time");
        }else {
            if (orderByAsc){
                queryWrapper.orderByAsc(oderByField);
            }else {
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<FlightActivityDaily> flightActivityDailyPage1 = flightActivityDailyMapper.selectPage(flightActivityDailyPage, queryWrapper);
        return new PageVo<>(flightActivityDailyPage1);
    }
}
